package com.cwn.wethink.common.utils.rar;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Date;

import net.sf.sevenzipjbinding.IOutArchive;
import net.sf.sevenzipjbinding.IOutCreateArchiveZip;
import net.sf.sevenzipjbinding.IOutCreateCallback;
import net.sf.sevenzipjbinding.IOutItemZip;
import net.sf.sevenzipjbinding.ISequentialInStream;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.OutItemFactory;
import net.sf.sevenzipjbinding.impl.RandomAccessFileOutStream;
import net.sf.sevenzipjbinding.util.ByteArrayStream;

public class CompressMessage {
    static class MyCreateCallback implements IOutCreateCallback<IOutItemZip> {
        private final byte[] bytesToCompress;
        private MyCreateCallback(byte[] bytesToCompress) {
            this.bytesToCompress = bytesToCompress;
        }
        public void setOperationResult(boolean operationResultOk) { }
        public void setTotal(long total) { }
        public void setCompleted(long complete) { }
        public IOutItemZip getItemInformation(int index, OutItemFactory<IOutItemZip> outItemFactory) {
            IOutItemZip outItem = outItemFactory.createOutItem();
            outItem.setDataSize((long) bytesToCompress.length);
            outItem.setPropertyPath("message.txt");
            outItem.setPropertyCreationTime(new Date());
            return outItem;
        }
        public ISequentialInStream getStream(int index) {
            return new ByteArrayStream(bytesToCompress, true);
        }
    }
    public static void main(String[] args) {
//        compress(args);
        compress22();
    }

    private static void compress(String[] args) {
        if (args.length!= 2) {
            System.out.println("Usage: java CompressMessage <archive> <msg>");
            return;
        }
        final byte[] bytesToCompress = args[1].getBytes();
        RandomAccessFile raf = null;
        IOutCreateArchiveZip outArchive = null;
        try {
            raf = new RandomAccessFile(args[0], "rw");
            outArchive = SevenZip.openOutArchiveZip();
            outArchive.setLevel(5);
            outArchive.createArchive(new RandomAccessFileOutStream(raf), 2, new MyCreateCallback(bytesToCompress));
            System.out.println("Compression operation succeeded");
        } catch (SevenZipException e) {
            System.err.println("7-Zip-JBinding-Error:");
            e.printStackTraceExtended();
        } catch (Exception e) {
            System.err.println("Error occurs: " + e);
        } finally {
            if (outArchive!= null) {
                try {
                    outArchive.close();
                } catch (IOException e) {
                    System.err.println("Error closing archive: " + e);
                }
            }
            if (raf!= null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    System.err.println("Error closing file: " + e);
                }
            }
        }
    }

    private static void compress22() {

        final byte[] bytesToCompress = "222233344".getBytes();
        RandomAccessFile raf = null;
        IOutCreateArchiveZip outArchive = null;
        try {
            raf = new RandomAccessFile("out.rar", "rw");
            outArchive = SevenZip.openOutArchiveZip();
            outArchive.setLevel(5);
            outArchive.createArchive(new RandomAccessFileOutStream(raf), 1, new MyCreateCallback(bytesToCompress));
            System.out.println("Compression operation succeeded");
        } catch (SevenZipException e) {
            System.err.println("7-Zip-JBinding-Error:");
            e.printStackTraceExtended();
        } catch (Exception e) {
            System.err.println("Error occurs: " + e);
        } finally {
            if (outArchive!= null) {
                try {
                    outArchive.close();
                } catch (IOException e) {
                    System.err.println("Error closing archive: " + e);
                }
            }
            if (raf!= null) {
                try {
                    raf.close();
                } catch (IOException e) {
                    System.err.println("Error closing file: " + e);
                }
            }
        }
    }
}